/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import controller.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mirza
 */
abstract public class DatabaseModel implements DatabaseConnection {
    protected Connection conn;
    protected Statement statement;

    public DatabaseModel(){
        try {
            setDatabaseConnection("com.mysql.jdbc.Driver", "localhost", "pergudangan", "root", "");
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setDatabaseConnection(String databaseProvider, String hostName, String databaseName, String userName, String password) {
        try {
            Class.forName(databaseProvider);
            conn = DriverManager.getConnection("jdbc:mysql://" + hostName + "/" + databaseName, userName, password);
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Connection getConnection() {
        if (conn != null) {
            return conn;
        }
        return null;
    }

    abstract boolean insertTransaction();
    abstract boolean updateTransaction();
    abstract boolean deleteTransaction();
    abstract ResultSet getData(String filter);
}
